<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Zookeeper, 鸟鸟">
    <meta name="description" content="大家好，我是鸟鸟，今年20岁，喜欢打篮球，搭建的这个博客是用来记录平时的一些学习笔记和心得">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Zookeeper | 鸟鸟</title>
    <link rel="icon" type="image/png" href="/yueyueniao-gif/favicon.png">

    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/css/my.css">

    <script src="/yueyueniao-gif/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/yueyueniao-gif/atom.xml" title="鸟鸟" type="application/atom+xml">
<link rel="stylesheet" href="/yueyueniao-gif/css/prism-tomorrow.css" type="text/css"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/yueyueniao-gif/" class="waves-effect waves-light">
                    
                    <img src="/yueyueniao-gif/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">鸟鸟</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/yueyueniao-gif/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">鸟鸟</div>
        <div class="logo-desc">
            
            大家好，我是鸟鸟，今年20岁，喜欢打篮球，搭建的这个博客是用来记录平时的一些学习笔记和心得
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/blinkfox/hexo-theme-matery" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/blinkfox/hexo-theme-matery" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/yueyueniao-gif/medias/featureimages/4.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Zookeeper</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/yueyueniao-gif/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    .toc-fixed .toc-link::before{
        position: fixed!important;/*当toc的位置改为fixed时，.toc-link::before也要改为fixed*/
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/yueyueniao-gif/tags/zookeeper/">
                                <span class="chip bg-color">zookeeper</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/yueyueniao-gif/categories/zookeper/" class="post-category">
                                zookeper
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-12-15
                </div>
                

                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    2.2k
                </div>
                

                

                
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="Zookeeper"><a href="#Zookeeper" class="headerlink" title="Zookeeper"></a>Zookeeper</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><ul>
<li>Zookeeper是一个开源的分布式的，为分布式应用提供协调服务的Apache项目</li>
<li>Zookeeper从设计模式的角度来理解，是一个基于观察者模式设计的分布式的服务管理框架，它<strong>负责存储和管理大家都关心的数据</strong>，然后<strong>接受观察者的注册，一旦这些数据的状态发生变化，Zookeeper就将负责同志已经在Zookeeper上注册的那些观察者做出相应的反应</strong></li>
</ul>
<h3 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h3><ul>
<li>Zookeeper：一个领导者，多个跟随着组成的集群。</li>
<li>集群中只要有半数以上节点存活，Zookeeper集群就能正常服务。</li>
<li>全局数据一致：每个Server保存一份相同的数据副本。Client无论连接到哪个Server，数据都是一致的。</li>
<li>更新请求顺序进行，来自同一个Client的更新请求按照其发送顺序依次执行。</li>
<li>数据更新原子性，一次数据更新要么成功，要么失败。</li>
<li>实时性，在一定时间范围内，Client能读到最新数据。</li>
</ul>
<h3 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h3><ul>
<li>Zookeeper数据模型的结构与Liunx文件系统很类似，整体上可以看做是一颗树，每个节点称作一个ZNode。每一个ZNode默认能够存储1MB的数据，每个XNode都可以通过其路径唯一标识。</li>
<li><a target="_blank" rel="noopener" href="https://imagelol.com/image/Fabk4"><img src="https://s3.jpg.cm/2020/12/15/Fabk4.png" alt="Fabk4.png"></a></li>
</ul>
<h2 id="单机版安装"><a href="#单机版安装" class="headerlink" title="单机版安装"></a>单机版安装</h2><h3 id="安装前准备"><a href="#安装前准备" class="headerlink" title="安装前准备"></a>安装前准备</h3><ol>
<li>安装jdk</li>
<li>拷贝Zookeeper安装包到Liunx系统下</li>
<li>解压到指定目录</li>
</ol>
<h3 id="修改配置文件"><a href="#修改配置文件" class="headerlink" title="修改配置文件"></a>修改配置文件</h3><ol>
<li><p>将zookeeper-3.4.10/conf下的zoo_sample.cfg修改为zoo.cfg</p>
<pre class=" language-lua"><code class="language-lua"><span class="token punctuation">[</span>yueyueniao@localhost <span class="token keyword">local</span><span class="token punctuation">]</span>$ mv zoo_sample<span class="token punctuation">.</span>cfg zoo<span class="token punctuation">.</span>cfg</code></pre>
</li>
<li><p>打开zoo.cfg文件，修改dataDir路径</p>
<pre class=" language-lua"><code class="language-lua"><span class="token punctuation">[</span>yueyueniao@localhost <span class="token keyword">local</span><span class="token punctuation">]</span>$ vim zoo<span class="token punctuation">.</span>cfg

修改内容如下
dataDir<span class="token operator">=</span><span class="token operator">/</span>opt<span class="token operator">/</span>zookeeper<span class="token operator">-</span><span class="token number">3.4</span><span class="token number">.10</span><span class="token operator">/</span>zkData</code></pre>
</li>
<li><p>在/opt/zookeeper-3.4/10/这个目录上创建zkData文件夹</p>
<pre class=" language-lua"><code class="language-lua"><span class="token punctuation">[</span>yueyueniao@localhost <span class="token keyword">local</span><span class="token punctuation">]</span>$ mkdir zkData</code></pre>
</li>
</ol>
<h3 id="操作Zookeeper"><a href="#操作Zookeeper" class="headerlink" title="操作Zookeeper"></a>操作Zookeeper</h3><ol>
<li><p>启动zookeeper</p>
<pre class=" language-lua"><code class="language-lua"><span class="token punctuation">[</span>yueyueniao@localhost <span class="token keyword">local</span><span class="token punctuation">]</span>$ bin<span class="token operator">/</span>zkServer<span class="token punctuation">.</span>sh start</code></pre>
</li>
<li><p>查看进程是否启动</p>
<pre class=" language-lua"><code class="language-lua"><span class="token punctuation">[</span>yueyueniao@localhost <span class="token keyword">local</span><span class="token punctuation">]</span>$ jps
<span class="token number">4020</span> Jps
<span class="token number">4001</span> QuorumPeerMain</code></pre>
</li>
<li><p>查看状态</p>
<pre class=" language-lua"><code class="language-lua"><span class="token punctuation">[</span>yueyueniao@localhost <span class="token keyword">local</span><span class="token punctuation">]</span>$ bin<span class="token operator">/</span>zkServer<span class="token punctuation">.</span>sh status</code></pre>
</li>
<li><p>启动客户端</p>
<pre class=" language-lua"><code class="language-lua"><span class="token punctuation">[</span>yueyueniao@localhost <span class="token keyword">local</span><span class="token punctuation">]</span>$ bin<span class="token operator">/</span>zkCli<span class="token punctuation">.</span>sh</code></pre>
</li>
<li><p>退出客户端</p>
<pre><code>[yueyueniao@localhost local]$ quit</code></pre>
</li>
<li><p>停止Zookeeper</p>
<pre class=" language-lua"><code class="language-lua"><span class="token punctuation">[</span>yueyueniao@localhost <span class="token keyword">local</span><span class="token punctuation">]</span>$ bin<span class="token operator">/</span>zkServer<span class="token punctuation">.</span>sh stop</code></pre>
</li>
</ol>
<h3 id="配置参数解读"><a href="#配置参数解读" class="headerlink" title="配置参数解读"></a>配置参数解读</h3><ol>
<li>tickTime =2000：通信心跳数，Zookeeper服务器与客户端心跳时间，单位毫秒<ul>
<li>Zookeeper使用的基本时间，服务器之间或客户端与服务器之间维持心跳的时间间隔，也就是每个tickTime时间就会发送一个心跳，时间单位为毫秒。</li>
<li>它用于心跳机制，并且设置最小的session超时时间为两倍心跳时间。(session的最小超时时间是2*tickTime)</li>
</ul>
</li>
<li>initLimit =10：LF初始通信时限<ul>
<li>集群中的Follower跟随者服务器与Leader领导者服务器之间初始连接时能容忍的最多心跳数（tickTime的数量），用它来限定集群中的Zookeeper服务器连接到Leader的时限。</li>
</ul>
</li>
<li>syncLimit =5：LF同步通信时限<ul>
<li>集群中Leader与Follower之间的最大响应时间单位，假如响应超过syncLimit * tickTime，Leader认为Follwer死掉，从服务器列表中删除Follwer。</li>
</ul>
</li>
<li>dataDir：数据文件目录+数据持久化路径<ul>
<li>主要用于保存Zookeeper中的数据。</li>
</ul>
</li>
<li>clientPort =2181：客户端连接端口<ul>
<li>监听客户端连接的端口。</li>
</ul>
</li>
</ol>
<h2 id="集群版安装"><a href="#集群版安装" class="headerlink" title="集群版安装"></a>集群版安装</h2><ul>
<li>真实的集群是需要部署在不同的服务器上的，但是我们在测试时启动多个虚拟机消耗内存太大，所以我们通常会搭建伪集群，也就是把所有的服务器搭建在一台虚拟机上，用端口进行区分。</li>
</ul>
<h3 id="安装前准备-1"><a href="#安装前准备-1" class="headerlink" title="安装前准备"></a>安装前准备</h3><ol>
<li><p>安装JDK</p>
</li>
<li><p>Zookeeper压缩包上传到服务器</p>
</li>
<li><p>将Zookeeper解压到/opt/zkcluster，重命名为zookeeper01。复制两份分别命名为zookeeper02和zookeeper03。</p>
<p>/opt/zookeeper-cluster/zookeeper01</p>
<p>/opt/zookeeper-cluster/zookeeper02</p>
<p>/opt/zookeeper-cluster/zookeeper03</p>
</li>
<li><p>在三个Zookeeper目录下创建data目录，并分别修改zookeeper/conf目录下的zoo_sample.cfg重命名为zoo.cfg。</p>
</li>
<li><p>修改zoo.cfg中dataDir=/opt/zookeeper-cluster/zookeeper01/data</p>
</li>
<li><p>修改zoo.cfg中的clientPort分别为2181 2182 2183</p>
<p>例如：修改/opt/zookeeper-cluster/zookeeper011/conf/zoo/cfg</p>
<pre><code>clientport=2181
dataDir=/usr/local/zookeeper-cluster/zookeeper01/data</code></pre>
</li>
</ol>
<h3 id="配置集群"><a href="#配置集群" class="headerlink" title="配置集群"></a>配置集群</h3><ol>
<li><p>在每个zookeeper的data目录下创建一个myid文件，内容分别是1,2,3.这个文件就是记录每个服务器的ID。</p>
</li>
<li><p>在每个zookeeper的zoo.cfg配置客户端访问端口（clientPort）和集群服务器IP列表。</p>
<pre><code>server.1=192.168.36.134:2881:3881
server.2=192.168.36.134:2882:3882
server.3=192.168.36.134:2883:3883
server.服务器ID=服务器IP地址：服务器之间通信端口：服务器之间投票选举端口</code></pre>
</li>
<li><p>依次启动三个zk实例，其中有一个leadr和两个follower</p>
</li>
</ol>
<h2 id="zookeeper基本使用"><a href="#zookeeper基本使用" class="headerlink" title="zookeeper基本使用"></a>zookeeper基本使用</h2><h3 id="命令行使用"><a href="#命令行使用" class="headerlink" title="命令行使用"></a>命令行使用</h3><p>通过zkClient进入zookeeper客户端命令行，输入help查看zookeeper客户端指令</p>
<p><img src="https://s3.jpg.cm/2020/12/16/FDvQ4.png" alt="FDvQ4.png"></p>
<ul>
<li><p>如上图列出了zookeeper所有客户端命令行，下面主要讲解常见的几个命令行</p>
<ul>
<li><p>使用ls命令来查看当前znode中包含的内容</p>
<pre><code>ls path [watch]</code></pre>
</li>
<li><p>查看当前节点数据并能看到更新次数等数据</p>
<pre><code>ls2 path [watch]</code></pre>
</li>
<li><p>创建节点-s含有序列-e临时</p>
<pre><code>create</code></pre>
</li>
<li><p>获得节点的值</p>
<pre><code>get path [watch]</code></pre>
</li>
<li><p>设置节点的值</p>
<pre><code>set</code></pre>
</li>
<li><p>查看节点状态</p>
<pre><code>stat</code></pre>
</li>
<li><p>删除节点</p>
<pre><code>delete</code></pre>
</li>
<li><p>递归删除节点</p>
<pre><code>rmr</code></pre>
</li>
</ul>
</li>
</ul>
<h3 id="api使用"><a href="#api使用" class="headerlink" title="api使用"></a>api使用</h3><ul>
<li><p>maven依赖</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
   <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.zookeeper<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>zookeeper<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>3.4.13<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></code></pre>
</li>
<li><p>api</p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/* 
创建zookeeper连接，connectString表示连接的zookeeper服务的地址，sessionTimeOut指定会话的超时时间，Watcher配置监听
*/</span>
    Zookeeper zk <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Zookeeper</span><span class="token punctuation">(</span>String connectString<span class="token punctuation">,</span><span class="token keyword">int</span> sessionTimeout<span class="token punctuation">,</span>Watcher watcher<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">/*
创建一个给定目录节点path，并给它设置数据，CreateMode标识有四种形式的目录节点，分别是
    PERSISTENT:持久化目录节点，这个目录节点存储的数据不会丢失;
    PERSISTENT_SEQUENTIAL:顺序自动编号的目录节点，这种目录节点会根据当前已经存在的节点数自动加1,然后返回给客户端已经     成功创建的目录节点名;
    EPHEMERAL:临时目录节点，一旦创建这个节点的客户端与服务器端口也就是session超时，这种节点会被动删除;
    EPHEMERAL_SEQUENTIAL:临时自动编号节点
*/</span>
    String <span class="token function">create</span><span class="token punctuation">(</span>String path<span class="token punctuation">,</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data<span class="token punctuation">,</span>List acl<span class="token punctuation">,</span>CreateMode createMode<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">/*
判断某个path是否存在，并设置是否监控这个目录节点，这里的watcher是在创建Zookeeper实例时指定的watcher,exists方法还有一个重载方法，可以指定特定的watcher
*/</span>
        Stat <span class="token function">exists</span><span class="token punctuation">(</span>String path<span class="token punctuation">,</span><span class="token keyword">boolean</span> watch<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">/*
重载方法，这里给某个目录节点设置特定的watcher，Watcher在Zookeeper是一个核心功能，Watcher可以监控目录节点的数据变化以及子目录的变化，一旦这些状态发生变化，服务器就会通知所有设置在这个目录节点上的Wathcer，从而每个客户端都很快知道它所关注的目录节点的状态发生变化，而做出相应的反应
*/</span>
        Stat <span class="token function">exists</span><span class="token punctuation">(</span>String path<span class="token punctuation">,</span>Watcher watch<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">/*
删除path对应的目录节点，version为-1可以匹配任何版本，也就删除了这个目录节点所有数据
*/</span>
        <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span>String path<span class="token punctuation">,</span><span class="token keyword">int</span> version<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">/*
获取指定path下的所有子目录节点，同样getChildren方法也有一个重载方法可以设置特定的watcher监控子节点的状态。
*/</span>
        List <span class="token function">getChildren</span><span class="token punctuation">(</span>String path<span class="token punctuation">,</span>nollean watcher<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">/*
给path设置数据，可以指定这个数据的版本号，如果version为-1则可以匹配任何版本
*/</span>   
        Stat <span class="token function">setData</span><span class="token punctuation">(</span>String path<span class="token punctuation">,</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data<span class="token punctuation">,</span><span class="token keyword">int</span> version<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">/*
获取这个path对应的目录节点存储的数据，数据的版本等信息可以通过stat来指定，同时还可以设置是否监控这个目录节点数据的状态
*/</span>        
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getData</span><span class="token punctuation">(</span>String path<span class="token punctuation">,</span><span class="token keyword">boolean</span> watcher<span class="token punctuation">,</span>Stat stat<span class="token punctuation">)</span></code></pre>
</li>
<li><p>例子</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">throws</span> Exception<span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//1.创建连接</span>
    Zookeeper zooKeeper<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Zookeeper</span><span class="token punctuation">(</span><span class="token string">"192.168.36.134"</span><span class="token punctuation">,</span><span class="token number">2000</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">Watcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">process</span><span class="token punctuation">(</span>WathcedEvent<span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"触发了"</span>watchedEvent<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"的事件"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//1.创建父节点</span>
String path<span class="token operator">=</span>zooKeeper<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>
    <span class="token string">"/itheima"</span><span class="token punctuation">,</span>
    <span class="token string">"itheimaValue"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    ZooDefs<span class="token punctuation">.</span>Ids<span class="token punctuation">.</span>OPEN_ACL_UNSAFE<span class="token punctuation">,</span>
    CreateMode<span class="token punctuation">.</span>PERSISTENT<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">//3.创建子节点</span>
    String childrenpath<span class="token operator">=</span>zooKeeper<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span> 
    <span class="token string">"/itheima/children"</span><span class="token punctuation">,</span>
    <span class="token string">"childrenValue"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    ZooDefs<span class="token punctuation">.</span>Ids<span class="token punctuation">.</span>OPEN_ACL_UNSAFE<span class="token punctuation">,</span>
    CreateMode<span class="token punctuation">.</span>PERSISTENT<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">//4.获取节点中的值（父节点和子节点）</span>
    <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data<span class="token operator">=</span>zooKeeper<span class="token punctuation">.</span><span class="token function">getData</span><span class="token punctuation">(</span><span class="token string">"/itheima"</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">,</span>null<span class="token punctuation">)</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    List<span class="token operator">&lt;</span>String<span class="token operator">></span> children<span class="token operator">=</span>zooKeeper<span class="token punctuation">.</span><span class="token function">getChildren</span><span class="token punctuation">(</span><span class="token string">"/itheima/children"</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span>String child<span class="token operator">:</span>children<span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
       System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>child<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//5.修改节点中的值</span>
    Stat stat<span class="token operator">=</span>zooKeeper<span class="token punctuation">.</span><span class="token function">setData</span><span class="token punctuation">(</span><span class="token string">"/itheima"</span><span class="token punctuation">,</span><span class="token string">"itheimaUodate"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>stat<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">//6.判断某个节点是否存在</span>
    Stat exists<span class="token operator">=</span>zooKeeper<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string">"/itheima/children"</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>exists<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">//7.删除节点</span>
    zooKeeper<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string">"/itheima/children"</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>
</li>
</ul>
<script>
        document.querySelectorAll('.github-emoji')
          .forEach(el => {
            if (!el.dataset.src) { return; }
            const img = document.createElement('img');
            img.style = 'display:none !important;';
            img.src = el.dataset.src;
            img.addEventListener('error', () => {
              img.remove();
              el.style.color = 'inherit';
              el.style.backgroundImage = 'none';
              el.style.background = 'none';
            });
            img.addEventListener('load', () => {
              img.remove();
            });
            document.body.appendChild(img);
          });
      </script>
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://gitee.com/yueyueniao-gif/yueyueniao-gif.git" rel="external nofollow noreferrer">Wang Yue Niao</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://gitee.com/yueyueniao-gif/yueyueniao-gif.git/yueyueniao-gif/2020/12/15/zookeeper/">https://gitee.com/yueyueniao-gif/yueyueniao-gif.git/yueyueniao-gif/2020/12/15/zookeeper/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="https://gitee.com/yueyueniao-gif/yueyueniao-gif.git" target="_blank">Wang Yue Niao</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/yueyueniao-gif/tags/zookeeper/">
                                    <span class="chip bg-color">zookeeper</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/yueyueniao-gif/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/yueyueniao-gif/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/yueyueniao-gif/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/yueyueniao-gif/2020/12/16/dubbo/">
                    <div class="card-image">
                        
                        
                        <img src="/yueyueniao-gif/medias/featureimages/6.jpg" class="responsive-img" alt="Dubbo">
                        
                        <span class="card-title">Dubbo</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            DubboDubbo核心概念Dubbo官网: http://dubbo.apache.org/en-us/index.html
Dubbo 是一款高性能、轻量级的开源Java RPC框架，它提供了三大核心能力：面向接口的远程方法调用，智能容
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-12-16
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/yueyueniao-gif/categories/Dubbo/" class="post-category">
                                    Dubbo
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/yueyueniao-gif/tags/Dubbo/">
                        <span class="chip bg-color">Dubbo</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/yueyueniao-gif/2020/12/10/windows-server012-bu-shu-xiang-mu/">
                    <div class="card-image">
                        
                        
                        <img src="/yueyueniao-gif/medias/featureimages/22.jpg" class="responsive-img" alt="Windows Server012部署专家库项目">
                        
                        <span class="card-title">Windows Server012部署专家库项目</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            总结安装
需要现在服务器上安装java环境，并且配置环境变量
安装mysql，配不配置环境变量都行，如果不配置的话，需要在mysql的bin目录下进行操作
安装redis，下载一个windows版本的redis，解压安装，到安装目录下找到一
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-12-10
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/yueyueniao-gif/categories/%E9%83%A8%E7%BD%B2%E9%A1%B9%E7%9B%AE/" class="post-category">
                                    部署项目
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/yueyueniao-gif/tags/%E9%83%A8%E7%BD%B2%E9%A1%B9%E7%9B%AE/">
                        <span class="chip bg-color">部署项目</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/yueyueniao-gif/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/yueyueniao-gif/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/yueyueniao-gif/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/yueyueniao-gif/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/yueyueniao-gif/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>


<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=default"></script>

    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/yueyueniao-gif/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="2566555574"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/yueyueniao-gif/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
       <h2>没有任何动物比蚂蚁更勤奋，然而它却最沉默寡言</h2><br><br>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/yueyueniao-gif/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/yueyueniao-gif/search.xml", 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/yueyueniao-gif/libs/materialize/materialize.min.js"></script>
    <script src="/yueyueniao-gif/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/yueyueniao-gif/libs/aos/aos.js"></script>
    <script src="/yueyueniao-gif/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/yueyueniao-gif/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/yueyueniao-gif/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/yueyueniao-gif/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/yueyueniao-gif/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    

    
    <script src="/yueyueniao-gif/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
